package com.f5.management.service.impl;

import com.f5.management.dao.BuildingDAO;
import com.f5.management.entity.Building;
import com.f5.management.service.BuildingService;

import com.f5.management.utils.PageResult;
import com.f5.management.utils.SerialUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {
    @Autowired
    private BuildingDAO buildingDAO;

    //添加
    @Override
    @Transactional
    public boolean add(Building building) {
        //自动生成编号    B1001
        int num = buildingDAO.count();
        //生成的 n + 1
        building.setSerialNumber(SerialUtil.BuildingSerial(num + 1));
        return buildingDAO.add(building) > 0 ? true : false;
    }

    //通过名字查询id
    @Override
    public int findBidByName(String name) {

        return buildingDAO.findBidByName(name);
    }

    //分页查询 首页展示
    @Override
    public PageResult<List<Building>> findByPage(int page, int size) {
        PageResult<List<Building>> result = new PageResult<List<Building>>();
        Page<Building> startPage = PageHelper.startPage(page, size);
        List<Building> buildingList = buildingDAO.findAll();
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(startPage.getTotal());
        result.setTotalPage(startPage.getPages());
        result.setData(buildingList);
        return result;
    }

    //更改信息
    @Override
    public boolean updateInfo(Building building) {
        return buildingDAO.updateInfo(building) > 0 ? true : false;
    }

    //查询编号是否存在
    @Override
    public boolean selectNumber(String serialNumber) {
        String oldNum = serialNumber;
        String newNum = buildingDAO.selectNumber(serialNumber);
        //查询编号是否存在
        if (!oldNum.equals(newNum)) {
            return true;
        } else {
            return false;
        }
    }

    //查询编号是否存在
    @Override
    public boolean selectName(String name) {
        String oldName = name;
        String newName = buildingDAO.selectName(name);
        //查询编号是否存在
        if (!oldName.equals(newName)) {
            return true;
        } else {
            return false;
        }
    }

    //更改状态
    @Override
    @Transactional
    public boolean updateStatus(int id, boolean status) {

        return buildingDAO.updateStatus(id, status) > 0 ? true : false;
    }

    //删除信息
    @Override
    public boolean delInfo(int id) {
        return buildingDAO.delInfo(id) > 0 ? true : false;
    }

    //id查询数据进行修改
    @Override
    public Building findById(int id) {
        return buildingDAO.findById(id);
    }

    //条件查询信息
    @Override
    public PageResult<List<Building>> findByCid(int cid, String name, String serialNumber, int page, int size) {
        PageResult<List<Building>> result = new PageResult<List<Building>>();
        Page<Building> pageHelper = PageHelper.startPage(page, size);
        List<Building> buildingList = buildingDAO.findByCid(cid, name, serialNumber);
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(pageHelper.getTotal());
        result.setTotalPage(pageHelper.getPages());
        result.setData(buildingList);
        return result;
    }
}

















